﻿using System;
using System.Collections.Generic;
using System.Text;
using TNI;
using System.Drawing;

namespace Graphics
{
    public class Texture
    {
        ImageTNI image;
        public Texture(string path)
        {
            image = new ImageTNI(path);
        }

        public Bitmap GetBitmapImage()
        {
            return image.GetBitmapImage();
        }

        public TNI.Color GetSphereColorAt(double x, double y, double z)
        {
            // u = sin0cosO = x / (sqrt(x^2 + y^2 + z^2))
            // v = sin0cosO = y / (sqrt(x^2 + y^2 + z^2))

            double u = x / Math.Sqrt((x * x) + (y * y) + (z * z));
            double v = y / Math.Sqrt((x * x) + (y * y) + (z * z));

            u = Math.Abs(u);
            v = Math.Abs(v);

            u = image.Weight * u;
            v = image.Heigth * v;
            int U = Convert.ToInt32(u);
            int V = Convert.ToInt32(v);

            int position = (V * image.Weight) + U;
            if (position < image.Data.Count)
            return image.Data[position];
            
            return image.Data[0];
        }

    }
}
